﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.UI;
using xraiding.Models;

namespace xraiding.ViewModels
{
    public class CharacterEditViewModel
    {
        private readonly XRaidingEntities _entities;
        private List<RoleInfo> _roles;

        public CharacterEditViewModel()
        {
            _entities = XRaidingRepository.GetXRaidingConnection();
            Roles = new List<RoleInfo>();
            _entities.Roles.ToList().ForEach(role => Roles.Add(new RoleInfo
            {
                RoleId = role.RoleId,
                Description = role.Description
            }));

            Factions = new List<FactionInfo>();
            _entities.Factions.ToList().ForEach(faction => Factions.Add(new FactionInfo(){FactionId = faction.FactionId, Name = faction.Name}));
        }

        public CharacterEditViewModel(int characterId) : this()
        {                        
            CharacterId = characterId;
            Initialize();
        }

        private void Initialize()
        {
            var character = _entities.Characters.Single(c => c.CharacterId == CharacterId);                        
            Character = character;
            Name = character.CharacterName;
            Realm = character.Realm;
            Class = character.ClassId;
            PreferredRole = character.PreferredRoleId.HasValue ? character.PreferredRoleId.Value : 0;
            SecondaryRole = character.SecondaryRoleId.HasValue ? character.SecondaryRoleId.Value : 0;
            Level = character.CharacterLevel.HasValue ? character.CharacterLevel.Value : 0;
            ItemLevel = character.ItemLevel.HasValue ? character.ItemLevel.Value : 0;
            SelectedFaction = character.FactionId;
        }

        public Character Character { get; set; }
        public int CharacterId { get; set; }

        [Required]
        [Display(Name = "Character Name")]
        public string Name { get; set; }

        public string Realm { get; set; }

        [Required]
        [Display(Name = "Faction")]
        public int SelectedFaction { get; set; }

        [Required]
        [Range(1, 10, ErrorMessage="You must select a class.")]
        public int Class { get; set; }

        [Display(Name = "Preferred Role")]
        public int PreferredRole { get; set; }

        [Display(Name = "Secondary Role")]
        public int SecondaryRole { get; set; }

        [Required]
        public int Level { get; set; }

        public int ItemLevel { get; set; }


        public List<ClassInfo> Classes
        {
            get
            {
                var classes = XRaidingRepository.GetClasses();
                classes.Insert(0, new ClassInfo() {ClassId = 0, Name = "Select class"});
                return classes;
            }
        }

        
        public List<RoleInfo> Roles
        {
            get { return _roles; }
            set { _roles = value; }
        }

        private List<FactionInfo> _factions;
        public List<FactionInfo> Factions
        {
            get { return _factions; }
            set { _factions = value; }
        }

        public void Save()
        {            
            if (CharacterId == 0)
            {
                Character = new Character
                                {
                                    UserId = XRaidingRepository.GetUserId()
                                };
                _entities.Characters.AddObject(Character);
            }
            else
            {
                Character = _entities.Characters.Single(c => c.CharacterId == CharacterId);
                _entities.Characters.Attach(Character);
            }

            Character.CharacterName = Name;
            Character.ClassId = Class;
            Character.PreferredRoleId = PreferredRole;
            Character.SecondaryRoleId = SecondaryRole;
            Character.CharacterLevel = Level;
            Character.ItemLevel = ItemLevel;
            Character.FactionId = SelectedFaction;
            Character.Realm = Realm;                                   
            _entities.SaveChanges();
        }

        public static void DeleteCharacter(int characterId)
        {
            var entities = XRaidingRepository.GetXRaidingConnection();
            var character = entities.Characters.Single(c => c.CharacterId == characterId);
            entities.Characters.DeleteObject(character);
            entities.SaveChanges();
        }
    }

    public class FactionInfo
    {
        public int FactionId { get; set; }
        public string Name { get; set; }
    }
}